package com.etogc.groupchat.group.adpt;

/**
 * Created by McGrady on 2017/6/11.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.etogc.groupchat.R;
import com.etogc.util.GcUrl;
import com.etogc.util.ImageUtils;
import com.etogc.util.YcToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.rong.imkit.RongIM;

/**
 * 来访记录适配器
 */
public class MyGroupAdapter extends RecyclerView.Adapter<MyGroupAdapter.MyViewHolder> {

    private final Context mContext;

    private List<HashMap<String, String>> mList = new ArrayList<>();

    public MyGroupAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<HashMap<String, String>> list) {
        if (list != null) {
            this.mList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.group_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Map<String, String> map = mList.get(position);
        String groupName = map.get("group_name");
        String photo = map.get("photo");
        final String groupID = map.get("id");
        final String status = map.get("audit_status");
        if ("2".equals(status)) {
            holder.tvStatus.setText("待审核");
        }
        String inviteCode = map.get("invite_code");
        ImageUtils.setRadiuImage(mContext, ImageUtils.getImageUrl(photo), holder.ivGroupHead);
        if (!TextUtils.isEmpty(groupName)) {
            holder.tvGroupName.setText(String.valueOf(groupName) + " ( 群ID : " + String.valueOf(groupID) + ")");
        } else {
            holder.tvGroupName.setText("未命名");
        }
        holder.tvInviteId.setText("邀请码:" + String.valueOf(inviteCode));

        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status.equals("2")) {
                    YcToastHelper.getInstance()._toast("您好,本群正在审核中,请稍后再试");
                } else {
                    try {
                        RongIM.getInstance().startGroupChat(mContext, groupID, "哈哈哈");
                    } catch (IllegalArgumentException e) {
                        YcToastHelper.getInstance()._toast("群号异常");
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGroupHead;
        TextView tvGroupName, tvInviteId, tvStatus;
        View item;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivGroupHead = itemView.findViewById(R.id.ivGroupHead);
            tvGroupName = itemView.findViewById(R.id.tvGroupName);
            tvInviteId = itemView.findViewById(R.id.tvInviteId);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            item = itemView.findViewById(R.id.rootView);
        }
    }
}